/*
 * Copyright 2020-2025 the original author or authors.
 * You cannot use this file unless authorized by the author.
 */
package org.ipig.proc.fun.obj;

import org.ipig.proc.unit.Mergeable;
import org.ipig.proc.unit.Valueable;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;

/**
 * 替换对象计算
 *
 * @author <a href="mailto:comchnts@163.com">chinats</a>
 * @version $$Id: ReplacedObj.java 3 2020-01-21 09:36:33Z ts <comchnts@163.com> $$
 * @since 1.0
 */
@NoArgsConstructor
public class ReplacedObj implements Mergeable, Valueable<Object>, Serializable {
    @Getter
    private Long count = 0L;//计数
    @Getter
    @Setter
    private boolean merged = true;
    @Getter
    private Object value = new Object();//结果

    public ReplacedObj(Object in) {
        value = in;
        count++;
    }

    @Override
    public Mergeable merge(Mergeable mergeable) {
        if (mergeable instanceof ReplacedObj) {
            ReplacedObj newMergeableObject = (ReplacedObj) mergeable;
            value = newMergeableObject;
            this.count = this.count + newMergeableObject.count;
        } else {
            throw new UnsupportedOperationException("Cannot merge " + this.getClass().getName() + " with " + mergeable.getClass().getName());
        }
        return this;
    }

    @Override
    public Mergeable deepClone() {
        ReplacedObj mergeable = new ReplacedObj();
        mergeable.count = this.count;
        mergeable.value = this.value;
        mergeable.setMerged(this.merged);
        return mergeable;
    }
}